/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.bean.impl;

import java.util.Iterator;

import com.genia.toolbox.basics.bean.IterableIterator;

/**
 * this class allow to use the new foreach syntax on an iterator. The drawback
 * is that it is not possible to iterate more than once on this object, as the
 * underlaying iterator will be modified by the iteration.
 * 
 * @param <E>
 *          the type of the data contained in this {@link Iterable}/{@link Iterator}
 */
public class IterableIteratorImpl<E>
    implements IterableIterator<E>
{

  /**
   * the {@link Iterator} to mimic.
   */
  private final Iterator<E> iterator;



  /**
   * constructor.
   * 
   * @param iterator
   *          the {@link Iterator} to mimic
   */
  public IterableIteratorImpl(Iterator<E> iterator)
  {
    this.iterator = iterator;
  }



  /**
   * Returns <tt>true</tt> if the iteration has more elements. (In other
   * words, returns <tt>true</tt> if <tt>next</tt> would return an element
   * rather than throwing an exception.)
   * 
   * @return <tt>true</tt> if the iterator has more elements.
   * @see java.util.Iterator#hasNext()
   */
  public boolean hasNext()
  {
    return iterator.hasNext();
  }



  /**
   * Returns an iterator over a set of elements of type T.
   * 
   * @return an Iterator.
   * @see java.lang.Iterable#iterator()
   */
  public Iterator<E> iterator()
  {
    return this;
  }



  /**
   * Returns the next element in the iteration. Calling this method repeatedly
   * until the {@link #hasNext()} method returns false will return each element
   * in the underlying collection exactly once.
   * 
   * @return the next element in the iteration.
   * @see java.util.Iterator#next()
   */
  public E next()
  {
    return iterator.next();
  }



  /**
   * Removes from the underlying collection the last element returned by the
   * iterator (optional operation). This method can be called only once per call
   * to <tt>next</tt>. The behavior of an iterator is unspecified if the
   * underlying collection is modified while the iteration is in progress in any
   * way other than by calling this method.
   * 
   * @see java.util.Iterator#remove()
   */
  public void remove()
  {
    iterator.remove();
  }

}
